<?php
class Tree {
	/**
	* 生成树型结构所需要的2维数组
	* @var array
	*/
	public $arr = array();

	/**
	* 生成树型结构所需修饰符号，可以换成图片
	* @var array
	*/
	public $icon = array('│','├','└');
	public $nbsp = "&nbsp;";
	public $ret = '';
	public $optiondata=array();/*下拉菜单其他数据<option $optionstr  value=''></option>*/
	public $showname='';//额外显示的内容
	/**
	* 构造函数，初始化类
	* @param array 2维数组，例如：
	* array(
	*      1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
	*      2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
	*      3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
	*      4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
	*      5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
	*      6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
	*      7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
	*      )
	*/
	public function  init($arr=array(),$pid="parentid",$id="id",$name='name'){
		$this->arr = $arr;
	   	$this->ret = '';
	   	$this->pid=$pid;
	   	$this->id=$id;
	   	$this->name=$name;
	   	return is_array($arr);
	}
	/**
	 * 其他参数
	 * @param  [type] $config [description] option array('key'=>'value','key1'=>'value1');
	 * @param  string $type   [description]
	 * @return [type]         [description]
	 */
	public function setconfing($config,$type='option'){
		if($type=='option'){/*下拉菜单其他数据<option key  value=''></option>*/
			$this->optiondata=$config;
		}
		if($type=='showname'){
			$this->showname=$config;
		}

	}
	/**
	* 得到子级数组
	* @param int
	* @return array
	*/
	public function get_child($topid=0){
		$a = $newarr = array();
		if(is_array($this->arr)){
			foreach($this->arr as $id => $a){
				if($a[$this->pid] == $topid) $newarr[$id] = $a;
			}
		}
		return $newarr ? $newarr : false;
	}
	//获取所有父级id
	public function get_parentid_all($curid,&$arrall=array()){
		if($curid==0){
			return $arrall;
		}
		if(is_array($this->arr)){
			foreach($this->arr as $k=>$v){
				if($v[$this->id]==$curid){
					$arrall[]=$v[$this->id];
					if($v[$this->pid]!=0){
						$this->get_parentid_all($v[$this->pid],$arrall);
					}
				}
			}
		}
		return $arrall;
	}
	/*
	$topid 顶部id;  当前选择项 ；不能选择项 ，间歇； $noselectedpid=array('parentid',array(0,1));及栏目id 不能选择父id值为0和1的栏目
	 */
	public function get_select($topid,$selectedid='',$noselectedpid=array(),$adds=''){
		$number=1;
		$child=$this->get_child($topid);
		if(is_array($child)){
			$total=count($child);
			foreach ($child as $key => $value) {
				$j=$k='';
				if($number==$total){
					$j.=$this->icon[2];//下级菜单只有最后一个
				}else{
					$j.=$this->icon[1];//下级菜单有多个
					$k = $adds ? $this->icon[0] : '';
				}

				$spacer = $adds ? $adds.$j : '';
				//默认选择项
				$selected=$selectedid==$value[$this->id]?'selected':'';
				//不能选择项
				$noselected='';
				if(!empty($noselectedpid)){
					$nokey=$noselectedpid[0];
					$novalue=$noselectedpid[1];
					if(in_array($value[$nokey],$novalue)){
						$noselected='disabled';
					}
				}
				$stroption='';
				if(isset($this->optiondata)){
					foreach($this->optiondata as $x=>$y){
						$stroption.=$x."=".$value[$y]." ";
					}
				}
				$showname='';
				if(isset($this->showname) && $this->showname!=''){
					$showname="(".$value[$this->showname].")";		
				}
				$this->ret.="<option ".$stroption." ".$noselected." ".$selected."  value='".$value[$this->id]."'>".$spacer.$value[$this->name].$showname."</option>";

				$nbsp = $this->nbsp;
				$this->get_select($value[$this->id], $selectedid, $noselectedpid, $adds.$k.$nbsp);
				$number++;
			}
		}
		return $this->ret;
	}
	public function getsortdata($topid=0,&$arr=array()){
		$child=$this->get_child($topid);
		if(is_array($child)){
			foreach($child as $k=>$v){
				$arr[]=$v;
				$this->getsortdata($v[$this->id],$arr);
			}
		}
		return $arr;
	}
	//更加站点栏目分类
	public function get_select_site($topid,$selectedid='',$noselectedpid=array(),$adds='',&$sitesid=array()){
		$number=1;
		$child=$this->get_child($topid);
		if(is_array($child)){
			$total=count($child);
			foreach ($child as $key => $value) {
				$j=$k='';
				if($number==$total){
					$j.=$this->icon[2];//下级菜单只有最后一个
				}else{
					$j.=$this->icon[1];//下级菜单有多个
					$k = $adds ? $this->icon[0] : '';
				}

				$spacer = $adds ? $adds.$j : '';
				//默认选择项
				$selected=$selectedid==$value[$this->id]?'selected':'';
				//不能选择项
				$noselected='';
				if(!empty($noselectedpid)){
					$nokey=$noselectedpid[0];
					$novalue=$noselectedpid[1];
					if(in_array($value[$nokey],$novalue)){
						$noselected='disabled';
					}
				}
				$stroption='';
				if(isset($this->optiondata)){
					foreach($this->optiondata as $x=>$y){
						$stroption.=$x."=".$value[$y]." ";
					}
				}
				$showname='';
				if(isset($this->showname) && $this->showname!=''){
					$showname=$value[$this->showname];		
				}
				if($value[$this->pid]==$topid){

					if(!in_array($value['sitesid'],$sitesid)){
						$this->ret.='<option class="sitesclass" disabled>站点:'.$showname.'</option>';
						$sitesid[]=$value['sitesid'];
					}
				}
				if($value['coltype']==0){$coltypelass='typeclass';}
				if($value['coltype']==1){$coltypelass='sysclass';}
				$this->ret.="<option ".$stroption." ".$noselected." ".$selected."  class='".$coltypelass."'  value='".$value[$this->id]."'>".$spacer.$value[$this->name]."</option>";

				$nbsp = $this->nbsp;
				$this->get_select_site($value[$this->id], $selectedid, $noselectedpid, $adds.$k.$nbsp,$sitesid);
				$number++;
			}
		}
		return $this->ret;
	}
	//文章列表搜索
	public function get_select_article($topid,$selectedid='',$noselectedpid=array(),$adds='',&$sitesid=array()){
		$number=1;
		$child=$this->get_child($topid);
		if(is_array($child)){
			$total=count($child);
			foreach ($child as $key => $value) {
				$j=$k='';
				if($number==$total){
					$j.=$this->icon[2];//下级菜单只有最后一个
				}else{
					$j.=$this->icon[1];//下级菜单有多个
					$k = $adds ? $this->icon[0] : '';
				}

				$spacer = $adds ? $adds.$j : '';
				//默认选择项
				$selected=$selectedid==$value[$this->id]?'selected':'';
				//不能选择项
				$noselected='';
				if(!empty($noselectedpid)){
					$nokey=$noselectedpid[0];
					$novalue=$noselectedpid[1];
					if(in_array($value[$nokey],$novalue)){
						$noselected='disabled';
					}
				}
				$stroption='';
				if(isset($this->optiondata)){
					foreach($this->optiondata as $x=>$y){
						$stroption.=$x."=".$value[$y]." ";
					}
				}
				$showname='';
				if(isset($this->showname) && $this->showname!=''){
					$showname=$value[$this->showname];		
				}
				if($value[$this->pid]==$topid){

					if(!in_array($value['sitesid'],$sitesid)){
						$this->ret.='<option disabled class="sitesclass">站点:'.$showname.'</option>';
						$sitesid[]=$value['sitesid'];
					}
				}
				if($value['coltype']==0){$coltypelass='typeclass';}
				if($value['coltype']==1){$coltypelass='sysclass';}
				$this->ret.="<option ".$stroption." ".$noselected." ".$selected."  class='".$coltypelass."'  value='".$value[$this->id]."'>".$spacer.$value[$this->name]."</option>";

				$nbsp = $this->nbsp;
				$this->get_select_site($value[$this->id], $selectedid, $noselectedpid, $adds.$k.$nbsp,$sitesid);
				$number++;
			}
		}
		return $this->ret;
	}
}
?>